package com.bsoft.ecommerce.service.impl;
import com.alibaba.fastjson.JSON;
import com.bsoft.ecommerce.account.AddressInfo;
import com.bsoft.ecommerce.common.TableId;
import com.bsoft.ecommerce.dao.EcommerceAddressDao;
import com.bsoft.ecommerce.entity.EcommerceAddress;
import com.bsoft.ecommerce.filter.AccessContext;
import com.bsoft.ecommerce.service.IAddressService;
import com.bsoft.ecommerce.vo.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private EcommerceAddressDao addressDao;
    //存储多个用户信息
    @Override
    public TableId createAddressInfo(AddressInfo addressInfo) {
        //参数抓包
        LoginUserInfo loginUserInfo = AccessContext.getLoginuserInfo();
              List<EcommerceAddress> ecommerceAddresses=   addressInfo.getAddressitem().stream()
                       .map(a -> EcommerceAddress.to(loginUserInfo.getId(),a))
                       .collect(Collectors.toList());
              List<EcommerceAddress> saveRecords= addressDao.saveAll(ecommerceAddresses);
              List<Long> ids= saveRecords.stream()
                      .map(EcommerceAddress::getId).collect(Collectors.toList());
              log.info("create address info :{} {}",loginUserInfo.getId());
        return new TableId( ids.stream().map(TableId.Id::new).collect(Collectors.toList()));
    }
    @Override
    public AddressInfo getCurrentAddressInfo() {
        LoginUserInfo loginUserInfo = AccessContext.getLoginuserInfo();
        List<EcommerceAddress> ecommerceAddresses= addressDao.findByUserId(loginUserInfo.getId());
        List<AddressInfo.AddressItem> addressItems = ecommerceAddresses.stream()
                .map(EcommerceAddress::toAddressItem)
                .collect(Collectors.toList());
        return new AddressInfo(loginUserInfo.getId(), addressItems);
    }
    @Override
    public AddressInfo getAddressInfoById(Long id) {
         EcommerceAddress ecommerceAddress= addressDao.findById(id).orElse(null);
     if(null==ecommerceAddress){
         throw  new RuntimeException("address is not exist");
     }
        return new AddressInfo(ecommerceAddress.getUserId(), Collections.singletonList(ecommerceAddress.toAddressItem()));
    }
    @Override
    public AddressInfo getAddressInfoByTableId(TableId tableId) {
        List<Long> ids= tableId.getIds().stream().map(TableId.Id::getId).collect(Collectors.toList());
        log.info("get address info by table id:[{}]", JSON.toJSONString(ids));
        List<EcommerceAddress> ecommerceAddresses= addressDao.findAllById(ids);
        if(CollectionUtils.isEmpty(ecommerceAddresses)){
             return new AddressInfo(-1L,Collections.emptyList());
        }
        List<AddressInfo.AddressItem >  addressItems= ecommerceAddresses.stream().map(EcommerceAddress::toAddressItem).collect(Collectors.toList());
        return new AddressInfo(ecommerceAddresses.get(0).getUserId(),addressItems);
    }
}
